<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

class Attachment extends AdminBase
{
    public function add()
    {
        return $this->fetch();
    }

    public function doAdd()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('editormd-image-file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move('./uploads');
            if($info){
                $add_data['extension']      =   $info->getExtension();
                $add_data['save_name']      =   $info->getSaveName();
                $add_data['filename']       =   $info->getFilename();
                $add_data['create_time']    =   time();
                $id = model('Attachment')->insertGetId($add_data);
                if ($id) {
                    return json_encode(['url'=>config('app_host') . 'uploads/' . $add_data['save_name'],'success'=>1]);
//                    return $this->success('添加成功', config('app_host') . '/uploads/' . $add_data['save_name'], $add_data);
                } else {
                    return $this->error(model('Attachment')->getError());
                }
            }else{
                // 上传失败获取错误信息
                return $this->error($file->getError());
            }
        }
    }
}